package com.aaa.inbound.controller;


import com.aaa.entity.base.Locator;
import com.aaa.entity.inbound.Warehousing;
import com.aaa.entity.inbound.WarehousingDetail;
import com.aaa.entity.stock.Stock;
import com.aaa.inbound.feign.BaseFeign;
import com.aaa.inbound.feign.StockFeign;
import com.aaa.inbound.service.WarehousingDetailService;
import com.aaa.inbound.service.WarehousingService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inbound/warehousing")
@Api(description = "入库单管理")
public class WarehousingController {

    @Autowired
    private StockFeign stockFeign;

    @Autowired
    private BaseFeign baseFeign;

    @Autowired
    private WarehousingService warehousingService;

    @Autowired
    private WarehousingDetailService warehousingDetailService;

    @PostMapping("/searchWarehousing/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询")
    public R searchWarehousing(@ApiParam(value = "当前页数") @PathVariable Integer page,
                               @ApiParam(value = "每页个数") @PathVariable Integer limit,
                               @ApiParam(value = "查询条件") @RequestBody Warehousing warehousing) {
        Page<Warehousing> warehousingPage = warehousingService.searchWarehousing(page, limit, warehousing);
        return R.ok().message("分页查询成功").code(200).data("page", warehousingPage);
    }

    @PostMapping("/saveWarehousing/{no}/{locatorid}")
    @ApiOperation(value = "入库")
    public R saveWarehousing(@ApiParam(value = "单号") @PathVariable String no,
                             @ApiParam(value = "货位id") @PathVariable Long locatorid) {
        return warehousingService.saveWarehousing(no, locatorid);
    }

    @PostMapping("/updWarehosing")
    @ApiOperation(value = "修改操作")
    public R updWarehosing(@RequestBody Warehousing warehousing) {
        boolean b = warehousingService.saveOrUpdate(warehousing);
        if (b){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }

    @PostMapping("/examineWarehosing")
    @ApiOperation(value = "审核操作")
    public R examineWarehosing(@RequestBody Warehousing warehousing) {
        Warehousing ww = warehousingService.getById(warehousing.getId());
        if (ww.getState().equals("2")){
            return R.error().code(600).message("已经审核过了，不能重复审核");
        }
        boolean b = warehousingService.saveOrUpdate(warehousing);
        if (b){
            //根据入库单id查询入库单
            Warehousing ware = warehousingService.getById(warehousing.getId());
            //根据货位id查询货位信息
            Locator locator = baseFeign.selectLocById(ware.getLocatorid());
            //创建stock实体
            Stock stock = new Stock();
            stock.setLocatorid(ware.getLocatorid());
            stock.setLocatorcode(ware.getLocatorcode());
            stock.setLocatorname(ware.getLocatorname());
            stock.setWarehouseid(ware.getWarehouseid());
            stock.setLocatorstate(locator.getState());
            //创建查询条件
            QueryWrapper<WarehousingDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",warehousing.getId());
            //根据条件查询入库单对应的商品信息
            List<WarehousingDetail> listDetail = warehousingDetailService.list(queryWrapper);
            //循环入库
            for (WarehousingDetail detail : listDetail){
                stock.setCommodityid(detail.getCommodityid());
                stock.setCommoditybar(detail.getCommoditybar());
                stock.setCommodityname(detail.getCommodityname());
                stock.setCqty(detail.getQty());
                stock.setUqty(detail.getQty());
                stock.setLqty(0);
                stockFeign.inboundStock(stock);
            }
            return R.ok().message("审核成功");
        }else {
            return R.error().message("审核失败");
        }
    }

    @PostMapping("cancelWarehosing")
    @ApiOperation(value = "撤销审核操作")
    public R cancelWarehosing(@RequestBody Warehousing warehousing){
        Warehousing ww = warehousingService.getById(warehousing.getId());
        if (!ww.getState().equals("2")){
            return R.error().code(600).message("订单未审核不能撤销审核");
        }
        boolean b = warehousingService.saveOrUpdate(warehousing);
        if (b){
            //根据入库单id查询入库单
            Warehousing ware = warehousingService.getById(warehousing.getId());
            //根据货位id查询货位信息
            Locator locator = baseFeign.selectLocById(ware.getLocatorid());
            //创建stock实体
            Stock stock = new Stock();
            stock.setLocatorid(ware.getLocatorid());
            stock.setLocatorcode(ware.getLocatorcode());
            stock.setLocatorname(ware.getLocatorname());
            stock.setWarehouseid(ware.getWarehouseid());
            stock.setLocatorstate(locator.getState());
            //创建查询条件
            QueryWrapper<WarehousingDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",warehousing.getId());
            //根据条件查询入库单对应的商品信息
            List<WarehousingDetail> listDetail = warehousingDetailService.list(queryWrapper);
            //循环入库
            String message = "";
            int code = 0;
            for (WarehousingDetail detail : listDetail){
                stock.setCommodityid(detail.getCommodityid());
                stock.setCommoditybar(detail.getCommoditybar());
                stock.setCommodityname(detail.getCommodityname());
                stock.setCqty(detail.getQty());
                stock.setUqty(detail.getQty());
                stock.setLqty(0);
                R r = stockFeign.cinboundStock(stock);
                if (r.getCode()==500){
                    message = r.getMessage();
                    code = r.getCode();
                    break;
                }
                message = r.getMessage();
                code = r.getCode();
            }
            if (code==500){
                return R.error().message(message);
            }
            return R.ok().message("撤销审核成功");
        }else{
            return R.error().message("撤销审核失败");
        }
    }


    @PostMapping("/delWarehousing/{id}")
    @ApiOperation(value = "根据ID删除")
    public R delWarehousing(@PathVariable Long id){
        Warehousing receive = warehousingService.getById(id);
        if (receive.getState().equals("2")){
            return R.error().code(500).message("订单已经审核，不能删除");
        }else{
            QueryWrapper<WarehousingDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid",receive.getId());
            boolean a = warehousingDetailService.remove(queryWrapper);
            boolean b = warehousingService.removeById(id);
            if (b) {
                return R.ok().message("删除成功").code(200);
            }else {
                return R.error().message("删除失败").code(500);
            }
        }
    }
}

